package com.dy.design_pattern_study.singleton_pattern.dome2;

import com.dy.design_pattern_study.utils.Utils;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-04-17
 * @Content 多线程客户端
 */
@Slf4j
public class ThreadClient implements Runnable{

    private static String adress = null;

    public static void main(String[] args) {
        ThreadClient mt = new ThreadClient();//这个最多随机20个的时候比较稳定
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Thread t1 = new Thread(mt, "t" + i );
            threadList.add(t1);
        }

        System.out.println("threadList = " + threadList.size());

        for (Thread thread : threadList) {
            thread.start();
        }


       /* Thread t1 = new Thread(mt, "t1");
        Thread t2 = new Thread(mt, "t2");
        Thread t3 = new Thread(mt, "t3");
        Thread t4 = new Thread(mt, "t4");
        Thread t5 = new Thread(mt, "t5");
        Thread t6 = new Thread(mt, "t6");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();*/
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        //log.info("Thread = " + name + "，访问");
        MultiThreadSingleton instance = MultiThreadSingleton.getInstance();
        //StaticSingleton instance = StaticSingleton.getInstance();
        if (adress == null){
            adress = Utils.getAddress(instance);
            log.info("Thread instance address = " + adress);
        } else if (!adress.equals(Utils.getAddress(instance))){
            log.info("Thread instance address different= " + Utils.getAddress(instance));
        }

    }
}
